import { Config } from "../../src/config/Config";
import { DbConfig } from "../../src/config/DbConfig";
import { RoomConfig } from "../../src/config/RoomConfig";
import { DaoBase } from "../../src/engine/dao/DaoBase";
import DateUtil from "../../src/engine/utils/DateUtil";
import { Logger } from "../../src/engine/utils/Logger";
import { MysqlClient } from "../../src/engine/utils/MySqlClient";
import { MysqlManager } from "../../src/engine/utils/MysqlManager";
import { GameResult } from "../model/GameResult";

export class RoomResultDao extends DaoBase {

    public static DB_NAME: string = DbConfig.Db_Name.db_admin;
    public static Tb_Name: string = "t_room_result";

    public static async insert_room_result(model: GameResult) {
        if (model.gameConf["roomType"] == RoomConfig.Room_Type.Create) {
            let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
            
            let sql: string = "insert into t_room_result (gameType, gameConf, playerList, room_id, clubid, time, bigWin, userid1,userid2,userid3,userid4,userid5) VALUES(?,?,?,?,?,?,?,?,?,?,?,?) ;"
            let sqlParams: Array<any> = [model.gameType, JSON.stringify(model.gameConf), JSON.stringify(model.playerList), model.room_id, model.clubid,
            DateUtil.formatDateStr(model.time), model.bigWin, model.userid1, model.userid2, model.userid3, model.userid4, model.userid5];
            let rows = await client.queryPromise(sql, sqlParams);
            Logger.log("insert_room_result====", rows)
            if (rows && rows["affectedRows"] > 0) {
                return rows["insertId"]
            } else {
                return 0
            }
        }
        return 0
    }

    public static async update_room_result(model: GameResult) {
        if (model.gameConf["roomType"] == RoomConfig.Room_Type.Create) {
            let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
            let sql: string = "update t_room_result set `playerList`=?,`time`=?, `bigWin`=? where `id`=? ;"
            let sqlParams: Array<any> = [JSON.stringify(model.playerList),
            DateUtil.formatDateStr(model.time), model.bigWin, model.id];
            let rows = await client.queryPromise(sql, sqlParams);
            Logger.log("update_room_result====", rows)
            if (rows && rows["affectedRows"] > 0) {
                return true
            } else {
                return false
            }
        }
    }

    public static async checkClearRoomResult(delete_time: number) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = "delete from t_room_result where time < ?"
        let sqlParams = [DateUtil.formatDateStr(delete_time)];
        await client.queryPromise(sql, sqlParams);
    }

    public static async getResultByGameTypeAndClubIdAndUserid(gameType: number, clubid: number, userid: number, page: number, pageSize: number, searchTime: number) {
        let sql: string = ""
        let sqlParams = []
        let startTime: number = searchTime;
        let endTime: number = startTime + Config.OneDay;
        let startTimeStr: string = DateUtil.formatDateStr(startTime)
        let endTimeStr: string = DateUtil.formatDateStr(endTime)
        // let start: number = (page - 1) * pageSize
        if (userid > 0) {
            sql = "select * from  t_room_result where `gameType`=? and `clubid`=? and (`userid1`=? or `userid2`=? or `userid3` =? or `userid4`=? or `userid5`=?) and `time` >= ? and `time`< ? "
            sqlParams = [gameType, clubid, userid, userid, userid, userid, userid, startTimeStr, endTimeStr];
        } else {
            sql = "select * from  t_room_result where `gameType`=? and `clubid`=? and `time` >= ? and `time`< ? "
            sqlParams = [gameType, clubid, startTimeStr, endTimeStr];
        }
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let rows = await client.queryPromise(sql, sqlParams);
        Logger.log("getResultByGameTypeAndClubIdAndUserid", sql, sqlParams, rows)
        if (rows && rows.length > 0) {
            return rows
        }
        return []
    }

    public static async getResultByGameTypeAndUserid(gameType: number, userid: number, page: number, pageSize: number, searchTime: number) {
        let sql: string = "select * from  t_room_result  where "
        let sqlParams = []
        let startTime: number = searchTime;
        let endTime: number = startTime + Config.OneDay;
        let startTimeStr: string = DateUtil.formatDateStr(startTime)
        let endTimeStr: string = DateUtil.formatDateStr(endTime)
        // let start: number = (page - 1) * pageSize
        if(gameType > 0){
            sql += "  `gameType`="+gameType +" and "
        }
        if (userid > 0) {
            sql += "   (`userid1`=? or `userid2`=? or `userid3` =? or `userid4`=? or `userid5`=?) and `time` >= ? and `time`< ? "
            sqlParams = [ userid, userid, userid, userid, userid, startTimeStr, endTimeStr];
        } else {
            sql += " `time` >= ? and `time`< ? "
            sqlParams = [ startTimeStr, endTimeStr];
        }
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let rows = await client.queryPromise(sql, sqlParams);
        Logger.log("getResultByGameTypeAndUserid", sql, sqlParams, rows)
        if (rows && rows.length > 0) {
            return rows
        }
        return []
    }
}